import { Controller } from "../../mvc/core/Controller";
import { ILuckAwardsController } from "./interfaces/ILuckAwardsController";
import { IView } from "../../mvc/interfaces/IView";
import { LuckAwardsModule } from "../../../modules/LuckAwardsModule";
import { LuckAwardsIconBtn } from "../view/luckAwards/LuckAwardsIconBtn";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { SDrawPlateReward } from "../../../Message/Game/SDrawPlateReward";
import { GameProxy } from "../mvc/GameProxy";
import { EffectManager } from "../manager/EffectManager";
import { ClockManager } from "../manager/ClockManager";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SDrawPlateConditionState } from "../../../Message/Game/SDrawPlateConditionState";
import { FilterConst } from "../../component/gconst/FilterConst";
import { ItemData } from "../resource/info/ItemData";
import { ILuckAwardsModule } from "../../../modules/interfaces/ILuckAwardsModule";
type int = number;
//class LuckAwardsController
    
    export  class LuckAwardsController extends Controller implements ILuckAwardsController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._luckModule == null) 
            {
                this._luckModule = new LuckAwardsModule();
            }
            return this._luckModule;
        }

        public get luckAwardsIcon(): LuckAwardsIconBtn
        {
            if (this._luckAwardsIcon == null) 
            {
                this._luckAwardsIcon = new LuckAwardsIconBtn();
            }
            return this._luckAwardsIcon;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.LuckAwardsOpen, this.onLuckAwardsOpen);
            Dispatcher.addEventListener(EventName.StartAwards, this.onStartAwards);
            Dispatcher.addEventListener(EventName.StartAwardsSuccess, this.onStartAwardsSuccessHandler);
            Dispatcher.addEventListener(EventName.GetAwards, this.onGetAwards);
            Dispatcher.addEventListener(EventName.GetAwardsSuccess, this.onGetAwardsSuccessHandler);
            Dispatcher.addEventListener(EventName.ReSetAwards, this.onReSetAwards);
            Dispatcher.addEventListener(EventName.ReSetAwardsSuccess, this.onReSetAwardsSuccessHandler);
            Dispatcher.addEventListener(EventName.LockSetLockStatusSuccess, this.onLockSetLockStatusSuccessHandler);
            Dispatcher.addEventListener(EventName.DebugBtn, this.onCom);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateDrawPlateInfo, this.updateLuckAwards);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateDrawPlateConditionState, this.onConditionCompleteHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateDrawPlateConditionProgress, this.onCompleteProgressHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateDrawPlateConditionInfo, this.onConditionCompleteInfo);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.LuckAwardsOpenInfo, this.onLuckAwardsInfoHandler);
            return;
        }

        /* internal  */onReSetAwardsSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SDrawPlateReward;
            if (!(this._luckModule == null) && !this._luckModule.isHide) 
            {
                this._luckModule.reSetAwards(loc1);
            }
            return;
        }

        /* internal  */onReSetAwards(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (loc1 == 12) 
            {
                GameProxy.active.reSetAwards();
            }
            return;
        }

        /* internal  */onCom(arg1: DataEvent): void
        {
            if (!(this._luckModule == null) && !this._luckModule.isHide) 
            {
                GameProxy.active.onDebug();
            }
            return;
        }

        /* internal  */onPackChangeHandler(arg1: Object): void
        {
            if (!(this._luckModule == null) && !this._luckModule.isHide) 
            {
                this._luckModule.onPackChange();
            }
            return;
        }

        /* internal  */onLockSetLockStatusSuccessHandler(arg1: DataEvent): void
        {
            if (!(this._luckModule == null) && !this._luckModule.isHide) 
            {
                this._luckModule.lockStatusSuccess();
            }
            return;
        }

        /* internal  */onGetAwardsSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (!(this._luckModule == null) && !this._luckModule.isHide) 
            {
                this._luckModule.getAwards(loc1);
            }
            EffectManager.glowFilterUnReg(this._luckAwardsIcon);
            this.luckAwardsIcon.clear();
            return;
        }

        /* internal  */onGetAwards(arg1: DataEvent): void
        {
            GameProxy.active.getAwards();
            return;
        }

        /* internal  */onStartAwardsSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SDrawPlateReward;
            if (!(this._luckModule == null) && !this._luckModule.isHide) 
            {
                this._luckModule.playAwards(loc1);
            }
            if (loc1.rewards[0].type == 3) 
            {
                this.luckAwardsIcon.setTime((loc1.endTime.time - ClockManager.instance.nowDate.time) / 1000);
            }
            if (loc1.rewards[0].type == 4) 
            {
                this.luckAwardsIcon.setTxt();
            }
            return;
        }

        /* internal  */onStartAwards(arg1: DataEvent): void
        {
            var loc1=arg1.data.str;
            GameProxy.active.startAwards(loc1);
            return;
        }

        /* internal  */onLuckAwardsInfoHandler(arg1: SActiveToClient): void
        {
            this._luckAwardsInfo = arg1 as SActiveToClient;
            cache.luckAwards.luckAwardsInfo = this._luckAwardsInfo;
            if (this._luckAwardsInfo.status) 
            {
                this.luckAwardsIcon.show();
            }
            else 
            {
                this.luckAwardsIcon.hide();
            }
            return;
        }

        /* internal  */onCompleteProgressHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDrawPlateConditionState;
            if (loc1 && !(this._luckModule == null)) 
            {
                this._luckModule.onProgress(loc1);
            }
            return;
        }

        /* internal  */onConditionCompleteHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDrawPlateConditionState;
            cache.luckAwards.conditionState = loc1;
            if (loc1.state != 1) 
            {
                EffectManager.glowFilterUnReg(this._luckAwardsIcon);
                this.luckAwardsIcon.clear();
            }
            else 
            {
                EffectManager.glowFilterReg(this._luckAwardsIcon, [FilterConst.topIconFlashFilter]);
                this.luckAwardsIcon.setTxt();
            }
            if (this._luckModule != null) 
            {
                this._luckModule.conditionCom(loc1);
            }
            return;
        }

        /* internal  */onConditionCompleteInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDrawPlateReward;
            if (loc1.rewards[0].type != 3) 
            {
                if (loc1.rewards[0].type != 4) 
                {
                    this.luckAwardsIcon.clear();
                }
                else 
                {
                    this.luckAwardsIcon.setTxt();
                }
            }
            else 
            {
                this.luckAwardsIcon.setTime((loc1.endTime.time - ClockManager.instance.nowDate.time) / 1000);
            }
            return;
        }

        /* internal  */updateLuckAwards(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDrawPlateReward;
            if (!(this._luckModule == null) && !this._luckModule.isHide) 
            {
                this._luckModule.upData(loc1);
            }
            return;
        }

        /* internal  */onLuckAwardsOpen(arg1: DataEvent): void
        {
            var loc1=null;
            if (arg1.data != null) 
            {
                loc1 = arg1.data as ItemData;
            }
            if (view.isHide) 
            {
                view.show();
                if (loc1) 
                {
                    this._luckModule.openLuckAwards(loc1);
                }
                GameProxy.active.openLuckAwards();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        private /* var */_luckModule: ILuckAwardsModule;

        private /* var */_luckAwardsIcon: LuckAwardsIconBtn;

        private /* var */_luckAwardsInfo: SActiveToClient;
    }
